<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use app\common\controller\AgentBaseController;
use app\common\controller\PaylogBaseController;
class Index extends AgentBaseController
{
    public function index()
    {
    	$agent_id = session('agent_id');
		$data = DB::table('info_agent')->where('id',$agent_id)->find();
		$data['balance'] 	 = substr($data['balance'],0,strlen($data['balance'])-2);
    	$data['score'] 	 = substr($data['score'],0,strlen($data['score'])-2);
    	$data['ad_money'] 	 = substr($data['ad_money'],0,strlen($data['ad_money'])-2);
		$this->assign('data',$data);
		
		$data1 = DB::table('info_oem')->where('id',$data['agency_id2'])->find();
		$this->assign('data1',$data1);
    	return $this->fetch();
    }
	//编辑地址
    function edit_address(){
    	if($_POST){
    		$data['longitude']=input("lng");
			$data['latitude']=input("lat");
			$data['address']=input("address");
    		$res=db('info_agent')->where('id',session("agent_id"))->update($data);
			if($res){
				return json(["code"=>200,'msg'=>'修改成功']);
			}else{
				return json(["code"=>400,'msg'=>'修改失败']);
			}
    	}
    	
    	$agent_info = db('info_agent')->where('id',session("agent_id"))->find();
    	$this->assign('agent_info',$agent_info);
		return $this->fetch();
    }
    
     //签约管理
     public function qianyue()
    {
    	$data=db('info_agent')->where('id',session("agent_id"))->find();
		$this->assign('data',$data);
		$account=db('sys_agent_verif')->where('id',session("agent_id"))->find();
		$this->assign('account',$account);
		$qianyue=db('agent_qianyue')->where('agent_id',session("agent_id"))->find();
		$this->assign('qianyue',$qianyue);
    	return $this->fetch();
    }
    
    public function qianyue_step1()
    {
    	$data=db('agent_qianyue')->where('agent_id',session("agent_id"))->find();
		$this->assign('data',$data);
		$typelist=db('sys_platform_type')->where('pid',0)->select();
		$this->assign('typelist',$typelist);
		if(request()->isPost()){
			$param['agent_type'] 	= input("agent_type");
			$param['agent_name1'] 	= input("agent_name1");
			$param['agent_name2'] 	= input("agent_name2");
			$param['trade_type'] 	= input("trade_type");
			$param['address'] 		= input("address");
			$param['service_tel'] 	= input("service_tel");
			$param['contacts'] 		= input("contacts");
			$param['job'] 			= input("job");
			$param['contact_tel'] 	= input("contact_tel");
			$param['email'] 		= input("email");
			if($data){
				$ress = db('agent_qianyue')->where('agent_id',session("agent_id"))->update($param);
			}else{
				$param['add_time'] 	= now_time();
				$param['agent_id'] 	= session("agent_id");
				$param['oem_id'] 	= session("oem_id");
				$ress = db('agent_qianyue')->insert($param);
			}
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
    	return $this->fetch();
    }
    public function qianyue_step2()
    {
    	$data=db('agent_qianyue')->where('agent_id',session("agent_id"))->find();
		$this->assign('data',$data);
		if(request()->isPost()){
			$param['regist_name'] 		= input("regist_name");
			$param['regist_address'] 	= input("regist_address");
			$param['license'] 			= input("license");
			$param['main_business'] 	= input("main_business");
			$param['business_time1'] 	= input("business_time1");
			$param['business_time2'] 	= input("business_time2");
			$param['legal_name'] 		= input("legal_name");
			$param['document_type'] 	= input("document_type");
			$param['document_no'] 		= input("document_no");
			$param['document_time1'] 	= input("document_time1");
			$param['document_time2'] 	= input("document_time2");
			$ress = db('agent_qianyue')->where('agent_id',session("agent_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
		}
    	return $this->fetch();
    }
    public function qianyue_step3()
    {
    	$data=db('agent_qianyue')->where('agent_id',session("agent_id"))->find();
		$this->assign('data',$data);
		if(request()->isPost()){
			$param['account_name'] 		= input("account_name");
			$param['bank_account'] 		= input("bank_account");
			$param['open_bank'] 		= input("open_bank");
			$param['branch_address'] 	= input("branch_address");
			$param['branch_name'] 		= input("branch_name");
			$ress = db('agent_qianyue')->where('agent_id',session("agent_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
		}
    	return $this->fetch();
    }
    public function qianyue_step4()
    {
    	$data=db('agent_qianyue')->where('agent_id',session("agent_id"))->find();
		$this->assign('data',$data);
		if(request()->isPost()){
			$param['ids_pic1'] 		= input("ids_pic1");
			$param['ids_pic2'] 		= input("ids_pic2");
			$param['license_pic'] 	= input("license_pic");
			$param['bank_pic'] 		= input("bank_pic");
			$param['door_pic'] 		= input("door_pic");
			$param['indoor_pic'] 	= input("indoor_pic");
			$ress = db('agent_qianyue')->where('agent_id',session("agent_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
		}
    	return $this->fetch();
    }
    public function qianyue_step5()
    {
    	$data=Db::table('agent_qianyue')
    	->alias('a')
    	->join('sys_platform_type w','a.trade_type = w.id')
    	->field('a.*,w.type_name type_name')
    	->where('a.agent_id',session("agent_id"))
    	->find();
		$this->assign('data',$data);
    	return $this->fetch();
    }
    public function qiyong()
    {
    	$param['wx_way'] = input("lid");
    	$param['ali_way'] = input("lid");
		$ress = db('info_agent')->where('id',session("agent_id"))->update($param);
		if($ress){
			return json(['code'=>200,'msg'=>'修改成功']);
		}else{
			return json(['code'=>402,'msg'=>'修改失败']);
		}
		
    }
    //余额记录
     public function yue_record()
    {
    	if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
    	$where['agent_id']=session("agent_id");
		$where['order_type']=3;
		$list=db('log_agent_charge')->where($where)->order('add_time desc')->paginate(10);
		$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //鼓励金记录
     public function score_record()
    {
    	if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
    	$where['agent_id']=session("agent_id");
    	$where['order_type']=['not in',8];
		$list=db('log_agent_score')->where($where)->order('add_time desc')->paginate(20);
		$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //广告费记录
     public function ad_record()
    {
    	if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
    	$where['agent_id']=session("agent_id");
    	$where['order_type']=['in','7,8'];
		$list=db('log_agent_charge')->where($where)->paginate(20);
		$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    
    //鼓励金转余额
     public function score_yue()
    {
    	$agent=db('info_agent')->where('id',session("agent_id"))->find();
    	if($_POST){
			$ex_num=input("ex_num");
			if($agent['score']<=$ex_num){
				return json(["code"=>400,'msg'=>'兑现积分不足']);
			}
			$fee=0.05;//服务费率
			$ex_price=$ex_num*(1-$fee)/10;//鼓励金转余额多少
			$sql="update info_agent set score = score-$ex_num,balance = balance + $ex_price ";
			$res=DB::execute($sql);
			if($res){
				$data = array(
				'agent_id'		=> $agent['id'],
				'order_type' 	=> 6,
				'message' 	 	=> '鼓励金转余额',
			    'balance' 	 	=> $ex_price,
			    "store_balance"	=> $agent['balance'],
			    'pay_type' 	 	=> 5,
			    'status_tinyint'=> 1,
			    'oem_id' 	 	=> $agent['oem_id'],
			    'agency_id' 	=> $agent['agency_id1'],
			    'city_id' 	 	=> $agent['agency_id2'],
			    "add_time"	 	=> now_time()
				);
				db('log_agent_charge')->insert($data);
				$data = array(
					'agent_id'		=> $agent['id'],
					'order_type' 	=> 6,
					'message' 	 	=> "鼓励金转余额扣除鼓励金",
				    'balance' 	 	=> $ex_num,
				    'status_tinyint'=> 1,
				    'pay_type' 	 	=> 5,
				    'oem_id' 	 	=> $agent['oem_id'],
				    'agency_id' 	=> $agent['agency_id1'],
				    'city_id' 	 	=> $agent['agency_id2'],
				    "add_time"	 	=> now_time()
				);
				db('log_agent_score')->insert($data);
				return json(['code'=>200,'msg'=>'操作成功']);
			}else{
				return json(['code'=>400,'msg'=>'操作失败']);
			}
    	}
		$this->assign('agent',$agent);
    	return $this->fetch();
    }
    //鼓励金充值
     public function yue_score()
    {
    	$data=db('info_agent')->where('id',session("agent_id"))->find();
    	
    	$code_url="http://".$_SERVER['HTTP_HOST']."/index/pay/charge_score?agent_id=".$data['id'].'&charge_type=1&';
    	
    	$data['code_url'] = $code_url;
    	$this->assign('data',$data);
    	$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //广告费充值
     public function yue_ad()
    {
    	$data=db('info_agent')->where('id',session("agent_id"))->find();
    	
    	$code_url="http://".$_SERVER['HTTP_HOST']."/index/pay/charge_score?agent_id=".$data['id'].'&charge_type=2&';
    	
    	$data['code_url'] = $code_url;
    	$this->assign('data',$data);
    	
    	return $this->fetch();
    }
    
    //修改登陆密码
    public function return_password()
    {
		if(request()->isPost()){
			$xinpwd['password']=md5_salt(input('xinpwd'));
			$jiupwd['password']=md5_salt(input('jiupwd'));
			$agentid=session('agent_id');
			$res=Db::table('info_agent')->where('id',$agentid)->where($jiupwd)->find();
			if($res){
				$ress=Db::table('info_agent')->where('id',$agentid)->update($xinpwd);
				if($ress){
				  return json(['code'=>200,'msg'=>'成功']);
			    }else{
			      return json(['code'=>402,'msg'=>'失败']);
			    }
			}else{
				 return json(['code'=>408,'msg'=>'旧密码不对']);
			}
		}			
			
	    	return $this->fetch();
    }
    //修改操作密码
    public function return_operationpwd()
    {
    	if(request()->isPost()){
			$agentid=session('agent_id');
			$xinpwd['password']=md5_salt(input('xinpwd'));
			$jiupwd['password']=md5_salt(input('jiupwd'));
			
			$agent_info = Db::table('info_agent')->where('id',$agentid)->find();
			if($agent_info['password'] == $jiupwd['password']){
				$result = Db::table('info_agent')->where('id', $agent_info['id'])->update(['password' => $xinpwd['password']]);
				if($result){
					return json(['code'=>200,'msg'=>'修改成功']);
				}else{
					return json(['code'=>402,'msg'=>'修改失败']);
				}
			}else{
				return json(['code'=>408,'msg'=>'旧密码错误']);
			}
		}
    	return $this->fetch();
    }
    
    public function show_agent()
    {
    	$data=db('info_agent')->where('id',session("agent_id"))->find();
		$this->assign('data',$data);
		if(request()->isPost()){
			$param['logo'] 	= input("logo");
			$param['agent_name'] 	= input("agent_name");
			$param['tel'] 	= input("tel");
			$param['action'] 	= input("action");
			$param['content'] 	= input("content");
			$param['start_time'] 	= input("start_time");
			$param['end_time'] 	= input("end_time");
			$ress = db('info_agent')->where('id',session("agent_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
    	return $this->fetch();
    }
    public function renzheng()
    {
    	$account=db('sys_agent_verif')->where('id',session("agent_id"))->find();
		$this->assign('account',$account);
		if(request()->isPost()){
			$param['id_card_front'] 	= input("id_card_front");
			$param['id_card_behind'] 	= input("id_card_behind");
			$param['license'] 	= input("license");
			$param['bank_card_front'] 	= input("bank_card_front");
			$param['user_name'] 	= input("user_name");
			$param['bank_name'] 	= input("bank_name");
			$param['bank_no'] 	= input("bank_no");
			$param['id_number'] 	= input("id_number");
			$param['reserve_mobile'] 	= input("reserve_mobile");
			$param['oem_id'] 	= session("oem_id");
			if($account){
				$param['state'] 	= 2;
				$ress = db('sys_agent_verif')->where('id',session("agent_id"))->update($param);
			}else{
				$param['add_time'] 	= now_time();
				$ress = db('sys_agent_verif')->insert($param);
			}
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
    	return $this->fetch();
    }
    
}
